<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改密码</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background-color: #f5f7fa;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;

        }

        h1 {
            font-size: 26px;
            margin-bottom: 30px;
            color: #333;
            font-weight: 600;
            text-align: center;
        }

        .form-container {
            background-color: white;
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 8px 30px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 379px;
        }

        .form-group {
            position: relative;
            margin-bottom: 25px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-size: 15px;
            color: #555;
            font-weight: 500;
        }

        input[type="password"] {
            width: 100%;
            padding: 14px;
            border: 1px solid #e0e0e0;
            border-radius: 6px;
            font-size: 15px;
            transition: all 0.3s;
            background-color: #f9f9f9;
        }

        input[type="password"]:focus {
            border-color: #4a90e2;
            outline: none;
            box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.2);
            background-color: white;
        }

        .password-toggle {
            position: absolute;
            right: 15px;
            top: 40px;
            cursor: pointer;
            color: #888;
            font-size: 14px;
            user-select: none;
        }

        .strength-bar-container {
            height: 6px;
            background-color: #eee;
            border-radius: 3px;
            margin-top: 10px;
            overflow: hidden;
        }

        .strength-bar {
            height: 100%;
            width: 0;
            transition: width 0.3s, background-color 0.3s;
        }

        .strength-label {
            font-size: 13px;
            margin-top: 5px;
            text-align: right;
        }

        .hint {
            font-size: 13px;
            color: #888;
            margin-top: 8px;
            line-height: 1.5;
        }

        .submit-btn {
            background: linear-gradient(90deg, #4a90e2, #36d1dc);
            color: white;
            border: none;
            padding: 14px;
            font-size: 16px;
            border-radius: 6px;
            cursor: pointer;
            width: 100%;
            margin-top: 20px;
            font-weight: 600;
            transition: all 0.3s;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        .submit-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
        }

        .loader {
            display: inline-block;
            width: 16px;
            height: 16px;
            border: 2px solid rgba(255,255,255,0.3);
            border-radius: 50%;
            border-top-color: white;
            animation: spin 1s ease-in-out infinite;
            margin-right: 8px;
            vertical-align: middle;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
<img src="../images/enroll/【哲风壁纸】原野-月-月色.png" class="peijing" style="position: absolute;z-index: -1;width: 2000px;height: 1000px">
<div class="form-container">
    <h1>修改密码</h1>

   <form>
       <input type="hidden" id="user_id" name="user_id" value="${user.user_id}">
       <div class="form-group">
           <label for="password">原密码</label>
           <input type="password" id="password" placeholder="请输入当前密码">

       </div>

       <div class="form-group">
           <label for="new-password">新密码</label>
           <input type="password" id="new-password" placeholder="8-16位字符，包含字母和数字"
                  oninput="checkPasswordStrength(this.value)">
           <div class="strength-bar-container">
               <div class="strength-bar" id="password-strength-bar"></div>
           </div>
           <div class="strength-label" id="password-strength-text">弱</div>
           <div class="hint">
               8-16个字符，请使用字母加数字或符号的组合密码。<br>
               不能单独使用字母、数字或符号。
           </div>
       </div>

       <div class="form-group">
           <label for="confirm-password">确认密码</label>
           <input type="password" id="confirm-password" placeholder="请再次输入新密码">

       </div>

       <button type="button" class="submit-btn" id="submit-btn">提交修改</button>
   </form>

</div>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
    function togglePassword(id) {
        const input = document.getElementById(id);
        const toggle = input.nextElementSibling;
        if (input.type === 'password') {
            input.type = 'text';
            toggle.textContent = '隐藏';
        } else {
            input.type = 'password';
            toggle.textContent = '显示';
        }
    }

    function checkPasswordStrength(password) {
        const strengthBar = document.getElementById('password-strength-bar');
        const strengthText = document.getElementById('password-strength-text');

        let strength = 0;
        if (password.length >= 8) strength += 1;
        if (password.match(/[a-z]/)) strength += 1;
        if (password.match(/[A-Z]/)) strength += 1;
        if (password.match(/[0-9]/)) strength += 1;
        if (password.match(/[^a-zA-Z0-9]/)) strength += 1;

        let width = 0;
        let color = '';
        let text = '';

        switch(strength) {
            case 0:
            case 1:
                width = 25;
                color = '#ff4444';
                text = '弱';
                break;
            case 2:
                width = 50;
                color = '#ffbb33';
                text = '中';
                break;
            case 3:
                width = 75;
                color = '#00C851';
                text = '强';
                break;
            case 4:
            case 5:
                width = 100;
                color = '#007E33';
                text = '非常强';
                break;
        }

        strengthBar.style.width = width + '%';
        strengthBar.style.backgroundColor = color;
        strengthText.textContent = text;
        strengthText.style.color = color;
    }

    $("#submit-btn").on("click", function(event) {
        debugger
        event.preventDefault();
        let password = document.getElementById("password").value;
        let newPassword = document.getElementById("new-password").value;
        let confirmPassword = document.getElementById("confirm-password").value;
        let user_id = document.getElementById("user_id").value;
        
        // 验证输入
        if(!password) {
            alert("请输入原密码");
            return;
        }
        if(!password || newPassword.length < 8) {
            alert("新密码必须至少8个字符");
            return;
        }
        if(newPassword !== confirmPassword) {
            alert("新密码和确认密码不一致");
            return;
        }
        
        // 显示加载状态
        $("#submit-btn").html('<span class="loader"></span> 处理中...');
        $("#submit-btn").prop("disabled", true);
        
        $.ajax({
            url: "<%=request.getContextPath()%>/changeController",
            type: "POST",
            data: {
                op: "xugai",
                password: password,
                newPassword: newPassword,
                confirmPassword:confirmPassword,
                user_id: user_id
            },
            success: function(response) {
                if(response === "success") {
                    alert("密码修改成功！");
                    window.location.reload();
                } else {
                    alert(response);
                }
            },
            error: function(xhr, status, error) {
                alert("请求出错: " + error);
            },
            complete: function() {
                $("#submit-btn").html("提交修改");
                $("#submit-btn").prop("disabled", false);
            }
        });
    });


</script>
</body>
</html>
